package com.joey.concurrency.example.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;

/**
 * @Author hyy
 **/
@Slf4j
public class CountDownLatchExample {

    /**
     * 计数器如果还存在值，则线程进入阻塞状态，直到计数器为0。
     */
   static CountDownLatch countDownLatch = new CountDownLatch(4);

    public static void main(String[] args) {
        Thread thread = new Thread(){
            public void run(){
                log.info("子线程:{}正在执行",Thread.currentThread().getName());
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                countDownLatch.countDown();
                log.info("子线程:{}执行完成",Thread.currentThread().getName());
            }
        };
        Thread thread2 = new Thread(){
            public void run(){
                log.info("子线程:{}正在执行",Thread.currentThread().getName());
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                countDownLatch.countDown();
                log.info("子线程:{}执行完成",Thread.currentThread().getName());
            }
        };
        thread.start();
        thread2.start();

        try {
            log.info("等待==============");
            countDownLatch.await();
            log.info("完毕==============");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }




}
